package com.example.software.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.software.entity.DepartmentEntity;
import com.example.software.utils.PageUtils;
import com.example.software.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.example.software.dao.MedicineDao;
import com.example.software.entity.MedicineEntity;
import com.example.software.service.MedicineService;


@Service("medicineService")
public class MedicineServiceImpl extends ServiceImpl<MedicineDao, MedicineEntity> implements MedicineService {


    @Autowired
    private MedicineDao medicineDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<MedicineEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.containsKey("medicineTypeId")) {

            Long medicineTypeId = (Long) params.get("medicineTypeId");
            System.out.println("medicineTypeId"+medicineTypeId);
            wrapper.eq(medicineTypeId != null, MedicineEntity::getMedicineTypeId, medicineTypeId);
        }

        if (params.containsKey("name")) {

            String name = (String) params.get("name");
            wrapper.like(StringUtils.isNotBlank(name), MedicineEntity::getName, name);
        }

        if (params.containsKey("state")) {

            String state = (String) params.get("state");
            wrapper.like(StringUtils.isNotBlank(state), MedicineEntity::getState, state);
        }

        IPage<MedicineEntity> page = this.page(
                new Query<MedicineEntity>().getPage(params),
                wrapper
        );


        return new PageUtils(page);
    }

    @Override
    public List<Long> getMedicineIdsByTypeId(Long id) {

        return medicineDao.getMedicineIdsByTypeId(id);
    }

}